package org.example.managementsystem.repository;

import org.example.managementsystem.model.ProviderApplication;
import org.example.managementsystem.model.ProviderComplaint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 供应商申请数据访问对象
 * @author 王振祎
 * @date 2025-05-21
 */
@Repository
public class ProviderApplicationDao {

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public ProviderApplicationDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 插入供应商申请
     * @param application 供应商申请对象
     * @return 插入的记录数
     */
    public int insertApplication(ProviderApplication application) {
        String sql = "INSERT INTO provider_admission_application (provider_name, factory_address, manager_name, manager_phone, " +
                "company_type, registered_capital, employee_count, established_year, profile, applicant_job_number, is_approved, status) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql,
                application.getProviderName(),
                application.getFactoryAddress(),
                application.getManagerName(),
                application.getManagerPhone(),
                application.getCompanyType(),
                application.getRegisteredCapital(),
                application.getEmployeeCount(),
                application.getEstablishedYear(),
                application.getProfile(),
                application.getApplicantJobNumber(),
                application.isApproved(),
                application.isStatus());
    }

    /**
     * 更新供应商申请状态
     * @param applicationId
     * @param isApproved
     * @param status
     * @return
     */
    public int updateApplicationStatus(int applicationId, boolean isApproved, boolean status) {
        String sql = "UPDATE provider_admission_application SET is_approved = ?, status = ? WHERE application_id = ?";
        return jdbcTemplate.update(sql, isApproved, status, applicationId);
    }

    /**
     * 更新供应商申请
     */
    public int updateApplication(ProviderApplication application) {
        String sql = "UPDATE provider_admission_application SET provider_name = ?, factory_address = ?, manager_name = ?, " +
                "manager_phone = ?, company_type = ?, registered_capital = ?, employee_count = ?, established_year = ?, " +
                "profile = ?, applicant_job_number = ?, is_approved = ?, status = ? WHERE application_id = ?";
        return jdbcTemplate.update(sql,
                application.getProviderName(),
                application.getFactoryAddress(),
                application.getManagerName(),
                application.getManagerPhone(),
                application.getCompanyType(),
                application.getRegisteredCapital(),
                application.getEmployeeCount(),
                application.getEstablishedYear(),
                application.getProfile(),
                application.getApplicantJobNumber(),
                application.isApproved(),
                application.isStatus(),
                application.getApplicationId());
    }


    /**
     * 获取所有供应商申请
     * @return
     */
    public List<ProviderApplication> getAllApplications() {
        String sql = "SELECT * FROM provider_admission_application";
        return jdbcTemplate.query(sql, new ProviderApplicationRowMapper());
    }

    /**
     * 获取未审批的供应商申请
     * @return
     */
     public List<ProviderApplication> getAllUnapprovedApplications() {
        String sql = "SELECT * FROM provider_admission_application WHERE status = 0";
        return jdbcTemplate.query(sql, new ProviderApplicationRowMapper());
    }

    /**
     * 根据ID查询供应商申请
     * @param applicationId
     * @return
     */
    public ProviderApplication getApplicationById(int applicationId) {
        String sql = "SELECT * FROM provider_admission_application WHERE application_id = ?";
        return jdbcTemplate.queryForObject(sql, new ProviderApplicationRowMapper(), applicationId);
    }


    /**
     * 供应商申请行映射器
     * @return
     */
    private static class ProviderApplicationRowMapper implements RowMapper<ProviderApplication> {
        @Override
        public ProviderApplication mapRow(ResultSet rs, int rowNum) throws SQLException {
            ProviderApplication application = new ProviderApplication();
            application.setApplicationId(rs.getInt("application_id"));
            application.setProviderName(rs.getString("provider_name"));
            application.setFactoryAddress(rs.getString("factory_address"));
            application.setManagerName(rs.getString("manager_name"));
            application.setManagerPhone(rs.getString("manager_phone"));
            application.setCompanyType(rs.getString("company_type"));
            application.setRegisteredCapital(rs.getDouble("registered_capital"));
            application.setEmployeeCount(rs.getInt("employee_count"));
            application.setEstablishedYear(rs.getInt("established_year"));
            application.setProfile(rs.getString("profile"));
            application.setApplicantJobNumber(rs.getString("applicant_job_number"));
            application.setApproved(rs.getBoolean("is_approved"));
            application.setStatus(rs.getBoolean("status"));
            return application;
        }
    }

    /**
     * 根据id删除申请
     */
    public int deleteApplication(int applicationId) {
        String sql = "DELETE FROM provider_admission_application WHERE application_id = ?";
        return jdbcTemplate.update(sql, applicationId);
    }


}
